package com.kamistoat.ruoyi.ruoyimodulessystem.controller;

import java.util.List;
import java.util.stream.Collectors;

import com.kamistoat.ruoyi.ruoyiapisystem.domain.SysLogininfor;
import com.kamistoat.ruoyi.ruoyiapisystem.entity.SysLogininforEntity;
import com.kamistoat.ruoyi.ruoyicommoncore.utils.StringUtils;
import com.kamistoat.ruoyi.ruoyicommoncore.utils.poi.ExcelUtil;
import com.kamistoat.ruoyi.ruoyicommoncore.web.controller.BaseController;
import com.kamistoat.ruoyi.ruoyicommoncore.web.domain.AjaxResult;
import com.kamistoat.ruoyi.ruoyicommoncore.web.page.TableDataInfo;
import com.kamistoat.ruoyi.ruoyicommonlog.annotation.Log;
import com.kamistoat.ruoyi.ruoyicommonlog.enums.BusinessType;
import com.kamistoat.ruoyi.ruoyicommonsecurity.annotation.InnerAuth;
import com.kamistoat.ruoyi.ruoyicommonsecurity.annotation.RequiresPermissions;
import com.kamistoat.ruoyi.ruoyimodulessystem.service.SysLogininforService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

import static com.kamistoat.ruoyi.ruoyicommoncore.utils.page.PageUtils.startPage;


/**
 * 系统访问记录
 *
 * @author kamistoat
 * @email linjianshanke@163.com
 * @date 2022-08-09 10:34:46
 */
@RestController
@RequestMapping("/logininfor")
public class SysLogininforController extends BaseController {
    @Autowired
    private SysLogininforService sysLogininforService;

    /**
     * 匹配列表
     */
    @RequiresPermissions("system:logininfor:list")
    @GetMapping("/list")
    public TableDataInfo list(SysLogininfor sysLogininfor) {
        startPage();
        List<SysLogininforEntity> sysLogininforEntityList = sysLogininforService.selectLogininforList(
                StringUtils.isNull(sysLogininfor) ? new SysLogininfor() : sysLogininfor);
        return getDataTable(entity2Vo(sysLogininforEntityList));
    }

    /**
     * 导出登录日志
     */
    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:logininfor:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLogininfor logininfor) {
        List<SysLogininforEntity> sysLogininforEntityList = sysLogininforService.selectLogininforList(logininfor);
        ExcelUtil<SysLogininfor> util = new ExcelUtil<SysLogininfor>(SysLogininfor.class);
        util.exportExcel(response, entity2Vo(sysLogininforEntityList), "登录日志");
    }

    /**
     * 根据ids批量删除登录日志
     *
     * @param infoIds
     * @return
     */
    @RequiresPermissions("system:logininfor:remove")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public AjaxResult remove(@PathVariable Long[] infoIds) {
        return toAjax(sysLogininforService.deleteLogininforByIds(infoIds));
    }

    /**
     * 清空系统中所有人的登录日志
     */
    @RequiresPermissions("system:logininfor:remove")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/clean")
    public AjaxResult clean() {
        sysLogininforService.cleanLogininfor();
        return AjaxResult.success();
    }

    /**
     * 添加一份登录日志。
     * 该方法的调用者一定不可能是游览器(因为肯定是登录成功后才记录登录日志).因此使用 @InnerAuth 注解，对方法的调用者进行验证。
     */
    @InnerAuth
    @PostMapping
    public AjaxResult add(@RequestBody SysLogininfor logininfor) {
        return toAjax(sysLogininforService.insertLogininfor(vo2Entity(logininfor)));
    }

    /**
     * Entity 和 Vo类的转换
     */
    private List<SysLogininfor> entity2Vo(List<SysLogininforEntity> sysLogininforEntityList) {
        return
                sysLogininforEntityList.stream().map(sysLogininforEntity -> {
                    SysLogininfor sysLogininfor = new SysLogininfor();
                    BeanUtils.copyProperties(sysLogininforEntity, sysLogininfor);
                    return sysLogininfor;
                }).collect(Collectors.toList());
    }

    private SysLogininfor entity2Vo(SysLogininforEntity sysLogininforEntity) {
        SysLogininfor sysLogininfor = new SysLogininfor();
        BeanUtils.copyProperties(sysLogininforEntity, sysLogininfor);
        return sysLogininfor;
    }

    private List<SysLogininforEntity> vo2Entity(List<SysLogininfor> sysLogininforList) {
        return
                sysLogininforList.stream().map(sysLogininfor -> {
                    SysLogininforEntity sysLogininforEntity = new SysLogininforEntity();
                    BeanUtils.copyProperties(sysLogininfor, sysLogininforEntity);
                    return sysLogininforEntity;
                }).collect(Collectors.toList());
    }

    private SysLogininforEntity vo2Entity(SysLogininfor sysLogininfor) {
        SysLogininforEntity sysLogininforEntity = new SysLogininforEntity();
        BeanUtils.copyProperties(sysLogininfor, sysLogininforEntity);
        return sysLogininforEntity;
    }

}
